<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 商家管理
 */
namespace app\smartbc\model;
use app\common\facade\Inform;
use think\Model;
use util\Util;

class SmartbcStore extends Model{
    
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;

    //加入的群组
    public function group(){
        return $this->belongsToMany('SmartbcStoreGroup','SmartbcStoreUnion','group_id','store_id');
    }

    //所属管理员
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','manage_uid');
    }

    //连锁门店
    public function chain(){
        return $this->hasMany('SmartbcStoreChain','store_id','id');
    }

    //搜索好店名称
    public function searchNameAttr($query,$value){
        if(!empty($value)){
            $query->where('name','like', '%'.$value .'%');
        }
    }

    //添加或编辑
    public static function edit($param){
        $data['manage_uid'] = trim($param['manage_uid']);
        $data['cate_id']    = trim($param['cate_id']);
        $data['cate_sid']   = trim($param['cate_sid']);
        $data['cate_name']  = trim($param['cate_name']);
        $data['name']       = trim($param['name']);
        $data['address']    = trim($param['address']);
        $data['longitude']  = trim($param['longitude']);
        $data['latitude']   = trim($param['latitude']);
        $data['telphone']   = trim($param['telphone']);
        $data['img']        = trim($param['img']);
        $data['mch_id']     = trim($param['mch_id']);
        $data['is_top']     = 0;
        $data['imgs']       = json_encode($param['imgs']);
        if(isset($param['id'])){
            self::update($data,['id'=>(int)$param['id']]);
            return $param['id'];
        }else{
            $data['create_time']       = time();
            $data['member_miniapp_id'] = $param['member_miniapp_id'];
            $data['sort']              = 0;
            return self::insertGetId($data);
        }
    }


     /**
     * 置顶或取消
     * @param integer $id
     */
    public static function isTop(int $id){
        $result = self::where(['id' => $id])->field('is_top')->find();
        $data['is_top'] = $result['is_top'] ? 0 : 1;
        return self::where('id',$id)->update($data);
    } 

     /**
     * 锁定或取消
     * @param integer $id
     */
    public static function isLock(int $id,$member_miniapp_id){
        $result = self::where(['id' => $id])->field('is_lock,manage_uid')->find();
        $data['is_lock'] = $result['is_lock'] ? 0 : 1;
        if($data['is_lock'] == 0) {
            //通知申请者到微信
            Inform::sms($result->manage_uid,$member_miniapp_id,['title' =>'业务进展通知','type' => '好店申请','content' =>'您的好店申请已经通过审核','state' => '成功']);
        }
        return self::where('id',$id)->update($data);
    } 

    //根据管理ID获取所属商户
    public static function manageStore(int $uid){
        return self::with(['chain'	=> function($query) {
            $query->field('id,store_id,title,address,telphone,longitude,latitude');
        }])->where(['manage_uid' => $uid])->field('id,name,address,img,telphone,longitude,latitude,mch_id')->find();  
    }

    /**
     * 读取商家详情(api)
     * @param integer 读取ID
     * @return json
     */
    public static function getView(array $where){
        $data = self::where($where)->find();
        if(empty($data)){
            return false;
        }else{
            if($data['latitude'] > 0 && $data['longitude'] > 0 ){
                $map = bdMap_to_txMap($data['latitude'],$data['longitude']);
                $data['longitude'] = $map['lng'];
                $data['latitude'] =  $map['lat'];;
            }
            $data['img'] = $data['img']."?x-oss-process=style/w100";
            $imgs = empty($data['imgs']) ? [] :json_decode($data['imgs']);
            $imga = [];
            foreach ($imgs as $key => $img) {
                $imga[$key] = $img."?x-oss-process=style/500";
            }
            $data['imgs'] = $imga;
            $data['create_time'] = util::ftime($data['create_time']);
            $data['tags'] = explode(',',$data['tags']); 
            $data['distance'] = '0km';   //经纬度距离判断
            $data['chains']   = $data['chains'];
            return $data;
        }
    }
}